﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace ZEFIT
{
    class MYSQLAdabterUser : Adabter
    {
        public override List<object> Select()
        {
            String conString = "SELECT ID, Vorname, Nachname, Benutzername, Email, Passwort, Rechte, Aktiv FROM tbl_benutzer;";
            List<object> reader = MYSQLConnecter.SelectRows(new MySqlCommand(conString));
            //List<User> userList = new List<User>();
            //foreach (List<object> tempList in controller.Select())
            //{
            //    User temp = new User();
            //    temp.Id = (int)tempList[0];
            //    temp.FirstName = (String)tempList[1];
            //    temp.LastName = (String)tempList[2];
            //    temp.UserName = (String)tempList[3];
            //    temp.Email = (String)tempList[4];
            //    temp.Password = (String)tempList[5];
            //    temp.IsAdmin = (bool)tempList[6];
            //    temp.IsActive = (bool)tempList[7];
            //    userList.Add(temp);
            //}
            return reader;
        }

        public override void Insert(ref Data data)
        {
            User user = (User)data;

            String conString = "INSERT INTO tbl_benutzer (Vorname, Nachname, Benutzername, Email, Passwort, Rechte, Aktiv) VALUES" +
                " ('" + user.FirstName + "', '" + user.LastName + "', '" + user.UserName + "', '" + user.Email + "', '" + user.Password + "', " + user.IsAdmin + ", " + user.IsActive + ");";
            user.Id = MYSQLConnecter.SendInsert(new MySqlCommand(conString));
            
        }

        public override void Update(Data data)
        {
            User user = (User)data;

            String conString = "UPDATE tbl_benutzer" +
                " SET Vorname = '" + user.FirstName + "', Nachname = '" + user.LastName + "', Benutzername = '" + user.UserName + "', Email = '" + user.Email + "', Passwort = '" + user.Password + "', Rechte = " + user.IsAdmin + ", Aktiv = " + user.IsActive + 
                " WHERE ID = " + user.Id + ";";
            MYSQLConnecter.Send(new MySqlCommand(conString));
        }

        public override void Delete(int id)
        {
            String conString = "DELETE FROM tbl_benutzer" +
                " WHERE ID = " + id + ";";
            MYSQLConnecter.Send(new MySqlCommand(conString));
        }
    }
}
